Sesion 24

EXPRESS

Creating our first Server with NODE JS

For more, read the Started documentation

For that, we follow the next steps:

rychy@popped:~/Documents/webProgress/Session 24 Express$mkdir node rychy@popped:~/Documents/webProgress/Session 24 Express$cd node/ rychy@popped:~/Documents/webProgress/Session 24 Express/node$touch server.js/ rychy@popped:~/Documents/webProgress/Session 24 Express/node$npm init/
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sensible defaults.
    
    See `npm help init` for definitive documentation on these fields
    and exactly what they do.
    
    Use `npm install <pkg>` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    package name: (nodeserver) 
    version: (1.0.0) 
    description: 
    entry point: (server.js) 
    test command: 
    git repository: 
    keywords: 
    author: 
    license: (ISC) 
    About to write to /home/rychy/Documents/STUDY time/2022 & 2023 Practice/webProgress/Session 24 Express/nodeServer/package.json:
    
    {
    "name": "nodeserver",
    "version": "1.0.0",
    "description": "",
    "main": "server.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC"
    }
    
    
    Is this OK? (yes) 
    
    
    ╭───────────────────────────────────────────────────────────────╮
    │
    │      New major version of npm available! 6.14.18 → 9.6.6
    │   Changelog: https://github.com/npm/cli/releases/tag/v9.6.6
    │               Run npm install -g npm to update!
    │
    ╰───────────────────────────────────────────────────────────────╯
                
rychy@popped:~/Documents/webProgress/Session 24 Express/node$npm install express rychy@popped:~/Documents/webProgress/Session 24 Express/node$node server.js
    Example app listening on port 3000
                

The code for the first hello world, is:


const express = require('express');

const app = express();
const port = 3000;

Get is a method for hadle the request from the browser, and the response is for respond or resolv the requested
app.get('/', (req, res) => {
    res.send("<h1>Hello World!</h1><h1>I'm Rychy</h1>");
});

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`);
});

At this point, i'm going work in the backend, so for see the fields and run the code, go to the Github Repositorie, for this especifíc folder look at the Session 24

Nodemon

Nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.

Nodemon does not require any additional changes to your code or method of development. nodemon is a replacement wrapper for node. To use nodemon, replace the word node on the command line when executing your script.

Either through cloning with git or by using npm (the recommended way):

  1. npm install -g nodemon # or using yarn: yarn global add nodemon
  2. And nodemon will be installed globally to your system path.
  3. You can also install nodemon as a development dependency:
  4. npm install --save-dev nodemon # or using yarn: yarn add nodemon -D

Node ModuleBODY parser

For read the data into the Form POST, or the request POST, we need to install another module of NodeJs

FOR THAT WE NEED BODY PARSER Look the docmentation for more